﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using DriveJoy.Models;

namespace DriveJoy.Helpers
{
    public class XmlUtils
    {
        public static bool ReadXML(string fileName,IsolatedStorageFile isoStorage,List<ItemStructure> listItem)
        {
            try
            {
                //Read from the Isolated Storage
                using (IsolatedStorageFileStream stream = isoStorage.OpenFile(fileName, FileMode.Open))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<ItemStructure>));
                    listItem = (List<ItemStructure>)serializer.Deserialize(stream);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool SaveXML(string fileName, IsolatedStorageFile isoStorage, List<ItemStructure> listItem)
        {
            // Write to the Isolated Storage
            try
            {
                XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
                xmlWriterSettings.Indent = true;
                using (IsolatedStorageFileStream stream = isoStorage.OpenFile(fileName, FileMode.OpenOrCreate))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<ItemStructure>));
                    using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
                    {
                        serializer.Serialize(xmlWriter, listItem);
                    }
                }
                return true;
            }
            catch {
                return false;
            }
        }
    }
}
